import { defineStore } from 'pinia'
import { login, getDeptList } from '@/api/user'
import { LoginParams, UserInfoModel } from '@/api/model/userModel'

export const useUserStore = defineStore({
  id: 'app-user',
  state: () => ({
    token: '',
    userId: '',
    userInfo: <UserInfoModel>{},
    deptList: []
  }),
  getters: {
    getToken(): string {
      return this.token
    },
    getUserId(): string {
      return this.userId
    }
  },
  actions: {
    resetState() {
      this.token = ''
      this.userId = ''
      this.deptList = []
      this.userInfo = {} as UserInfoModel
    },
    async login(params: LoginParams) {
      this.userInfo = await login(params)
      return this.userInfo
    },
    async getDeptList() {
      const data = await getDeptList()
      this.deptList = data.depts
      return this.deptList
    }
  }
})
